<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 正则表达式</h1>
        <p>正则表达式是一个特殊的字符序列，它能帮助你方便的检查一个字符串是否与某种模式匹配。</p><p>Python 自1.5版本起增加了re 模块，它提供 Perl 风格的正则表达式模式。</p>


        <p>re 模块使 Python 语言拥有全部的正则表达式功能。 </p>
        <p>compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。 </p>
        <p>re 模块也提供了与这些方法功能完全一致的函数，这些函数使用一个模式字符串做为它们的第一个参数。</p>
        <p>本章节主要介绍Python中常用的正则表达式处理函数。</p>
        <h2>re.match函数</h2>
        <p>re.match 尝试从字符串的起始位置匹配一个模式，如果不是起始位置匹配成功的话，match()就返回none。</p>
        <p><strong>函数语法</strong>：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">re</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="pln">pattern</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">,</span><span class="pln"> flags</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)</span></pre>
        <p>函数参数说明：</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:30%">参数</th><th>描述</th>
            </tr>
            <tr><td>pattern</td><td>匹配的正则表达式</td></tr>
            <tr><td>string</td><td>要匹配的字符串。</td></tr>
            <tr><td>flags</td><td>标志位，用于控制正则表达式的匹配方式，如：是否区分大小写，多行匹配等等。</td></tr>
            </tbody></table>
        <p>匹配成功re.match方法返回一个匹配的对象，否则返回None。</p>
        <p>我们可以使用group(num) 或  groups() 匹配对象函数来获取匹配表达式。</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:30%">匹配对象方法</th><th>描述</th>
            </tr>
            <tr><td>group(num=0)</td><td>匹配的整个表达式的字符串，group() 可以一次输入多个组号，在这种情况下它将返回一个包含那些组所对应值的元组。</td></tr>
            <tr><td>groups()</td><td>返回一个包含所有小组字符串的元组，从 1 到 所含的小组号。</td></tr>
            </tbody></table>
        <p>实例 1：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python</span><span class="pln">
</span><span class="com"># -*- coding: UTF-8 -*- </span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> re
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">re</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">'www'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'www.runoob.com'</span><span class="pun">).</span><span class="pln">span</span><span class="pun">())</span><span class="pln">  </span><span class="com"># 在起始位置匹配</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">re</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">'com'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'www.runoob.com'</span><span class="pun">))</span><span class="pln">         </span><span class="com"># 不在起始位置匹配</span></pre>
        <p>以上实例运行输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">None</span></pre>
        <p>实例 2：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> re

line </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Cats are smarter than dogs"</span><span class="pln">

matchObj </span><span class="pun">=</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="pln"> r</span><span class="str">'(.*) are (.*?) .*'</span><span class="pun">,</span><span class="pln"> line</span><span class="pun">,</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">M</span><span class="pun">|</span><span class="pln">re</span><span class="pun">.</span><span class="pln">I</span><span class="pun">)</span><span class="pln">

</span><span class="kwd">if</span><span class="pln"> matchObj</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"matchObj.group() : "</span><span class="pun">,</span><span class="pln"> matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">())</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"matchObj.group(1) : "</span><span class="pun">,</span><span class="pln"> matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">1</span><span class="pun">))</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"matchObj.group(2) : "</span><span class="pun">,</span><span class="pln"> matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">2</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"No match!!"</span><span class="pun">)</span></pre>
        <p>以上实例执行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">()</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  </span><span class="typ">Cats</span><span class="pln"> are smarter than dogs
matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  </span><span class="typ">Cats</span><span class="pln">
matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">2</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  smarter</span></pre>
        <h2>re.search方法</h2>
        <p>re.search 扫描整个字符串并返回第一个成功的匹配。</p>
        <p>函数语法：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">re</span><span class="pun">.</span><span class="pln">search</span><span class="pun">(</span><span class="pln">pattern</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">,</span><span class="pln"> flags</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)</span></pre>
        <p>函数参数说明：</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:30%">参数</th><th>描述</th>
            </tr>
            <tr><td>pattern</td><td>匹配的正则表达式</td></tr>
            <tr><td>string</td><td>要匹配的字符串。</td></tr>
            <tr><td>flags</td><td>标志位，用于控制正则表达式的匹配方式，如：是否区分大小写，多行匹配等等。</td></tr>
            </tbody></table>
        <p>匹配成功re.search方法返回一个匹配的对象，否则返回None。</p>
        <p>我们可以使用group(num) 或  groups() 匹配对象函数来获取匹配表达式。</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:30%">匹配对象方法</th><th>描述</th>
            </tr>
            <tr><td>group(num=0)</td><td>匹配的整个表达式的字符串，group() 可以一次输入多个组号，在这种情况下它将返回一个包含那些组所对应值的元组。</td></tr>
            <tr><td>groups()</td><td>返回一个包含所有小组字符串的元组，从 1 到 所含的小组号。</td></tr>
            </tbody></table>
        <p>实例 1：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> re

</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">re</span><span class="pun">.</span><span class="pln">search</span><span class="pun">(</span><span class="str">'www'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'www.runoob.com'</span><span class="pun">).</span><span class="pln">span</span><span class="pun">())</span><span class="pln">  </span><span class="com"># 在起始位置匹配</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">re</span><span class="pun">.</span><span class="pln">search</span><span class="pun">(</span><span class="str">'com'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'www.runoob.com'</span><span class="pun">).</span><span class="pln">span</span><span class="pun">())</span><span class="pln">         </span><span class="com"># 不在起始位置匹配</span></pre>
        <p>以上实例运行输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln">
</span><span class="pun">(</span><span class="lit">11</span><span class="pun">,</span><span class="pln"> </span><span class="lit">14</span><span class="pun">)</span></pre>
        <p>实例 2：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> re

line </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Cats are smarter than dogs"</span><span class="pun">;</span><span class="pln">

searchObj </span><span class="pun">=</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">search</span><span class="pun">(</span><span class="pln"> r</span><span class="str">'(.*) are (.*?) .*'</span><span class="pun">,</span><span class="pln"> line</span><span class="pun">,</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">M</span><span class="pun">|</span><span class="pln">re</span><span class="pun">.</span><span class="pln">I</span><span class="pun">)</span><span class="pln">

</span><span class="kwd">if</span><span class="pln"> searchObj</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"searchObj.group() : "</span><span class="pun">,</span><span class="pln"> searchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">())</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"searchObj.group(1) : "</span><span class="pun">,</span><span class="pln"> searchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">1</span><span class="pun">))</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"searchObj.group(2) : "</span><span class="pun">,</span><span class="pln"> searchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">2</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Nothing found!!"</span><span class="pun">)</span></pre>
        以上实例执行结果如下：
        <pre class="prettyprint prettyprinted" style=""><span class="pln">searchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">()</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  </span><span class="typ">Cats</span><span class="pln"> are smarter than dogs
searchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  </span><span class="typ">Cats</span><span class="pln">
searchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="lit">2</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  smarter</span></pre>
        <h2>re.match与re.search的区别</h2>
        <p>re.match只匹配字符串的开始，如果字符串开始不符合正则表达式，则匹配失败，函数返回None；而re.search匹配整个字符串，直到找到一个匹配。</p>
        <p>实例：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> re

line </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Cats are smarter than dogs"</span><span class="pun">;</span><span class="pln">

matchObj </span><span class="pun">=</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="pln"> r</span><span class="str">'dogs'</span><span class="pun">,</span><span class="pln"> line</span><span class="pun">,</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">M</span><span class="pun">|</span><span class="pln">re</span><span class="pun">.</span><span class="pln">I</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> matchObj</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"match --&gt; matchObj.group() : "</span><span class="pun">,</span><span class="pln"> matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"No match!!"</span><span class="pun">)</span><span class="pln">

matchObj </span><span class="pun">=</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">search</span><span class="pun">(</span><span class="pln"> r</span><span class="str">'dogs'</span><span class="pun">,</span><span class="pln"> line</span><span class="pun">,</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">M</span><span class="pun">|</span><span class="pln">re</span><span class="pun">.</span><span class="pln">I</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> matchObj</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"search --&gt; matchObj.group() : "</span><span class="pun">,</span><span class="pln"> matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"No match!!"</span><span class="pun">)</span></pre>
        以上实例运行结果如下：
        <pre class="prettyprint prettyprinted" style=""><span class="typ">No</span><span class="pln"> match</span><span class="pun">!!</span><span class="pln">
search </span><span class="pun">--&gt;</span><span class="pln"> matchObj</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">()</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  dogs</span></pre>

        <h2>检索和替换</h2>
        <p>Python 的re模块提供了re.sub用于替换字符串中的匹配项。</p>
        <p>语法：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">re</span><span class="pun">.</span><span class="kwd">sub</span><span class="pun">(</span><span class="pln">pattern</span><span class="pun">,</span><span class="pln"> repl</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">,</span><span class="pln"> count</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)</span></pre>
        <p>参数：</p>
        <ul>
            <li>pattern : 正则中的模式字符串。</li><li>
            repl : 替换的字符串，也可为一个函数。</li><li>
            string : 要被查找替换的原始字符串。</li><li>
            count : 模式匹配后替换的最大次数，默认 0 表示替换所有的匹配。</li></ul>
        <p>实例：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> re

phone </span><span class="pun">=</span><span class="pln"> </span><span class="str">"2004-959-559 # 这是一个电话号码"</span><span class="pln">

</span><span class="com"># 删除注释</span><span class="pln">
num </span><span class="pun">=</span><span class="pln"> re</span><span class="pun">.</span><span class="kwd">sub</span><span class="pun">(</span><span class="pln">r</span><span class="str">'#.*$'</span><span class="pun">,</span><span class="pln"> </span><span class="str">""</span><span class="pun">,</span><span class="pln"> phone</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"电话号码 : "</span><span class="pun">,</span><span class="pln"> num</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 移除非数字的内容</span><span class="pln">
num </span><span class="pun">=</span><span class="pln"> re</span><span class="pun">.</span><span class="kwd">sub</span><span class="pun">(</span><span class="pln">r</span><span class="str">'\D'</span><span class="pun">,</span><span class="pln"> </span><span class="str">""</span><span class="pun">,</span><span class="pln"> phone</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"电话号码 : "</span><span class="pun">,</span><span class="pln"> num</span><span class="pun">)</span></pre>
        以上实例执行结果如下：
        <pre class="prettyprint prettyprinted" style=""><span class="pun">电话号码</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  </span><span class="lit">2004</span><span class="pun">-</span><span class="lit">959</span><span class="pun">-</span><span class="lit">559</span><span class="pln">
</span><span class="pun">电话号码</span><span class="pln"> </span><span class="pun">:</span><span class="pln">  </span><span class="lit">2004959559</span></pre>
        <h3>repl 参数是一个函数</h3><p>
        以下实例中将字符串中的匹配的数字乘于 2：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> re

</span><span class="com"># 将匹配的数字乘于 2</span><span class="pln">
</span><span class="kwd">def</span><span class="pln"> </span><span class="kwd">double</span><span class="pun">(</span><span class="pln">matched</span><span class="pun">):</span><span class="pln">
    value </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">int</span><span class="pun">(</span><span class="pln">matched</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">(</span><span class="str">'value'</span><span class="pun">))</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">value </span><span class="pun">*</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln">

s </span><span class="pun">=</span><span class="pln"> </span><span class="str">'A23G4HFD567'</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">re</span><span class="pun">.</span><span class="kwd">sub</span><span class="pun">(</span><span class="str">'(?P&lt;value&gt;\d+)'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pun">,</span><span class="pln"> s</span><span class="pun">))</span></pre>
        <p>执行输出结果为：
        </p><pre class="prettyprint prettyprinted" style=""><span class="pln">A46G8HFD1134</span></pre>


        <h2>正则表达式修饰符 - 可选标志</h2>
        <p>正则表达式可以包含一些可选标志修饰符来控制匹配的模式。修饰符被指定为一个可选的标志。多个标志可以通过按位 OR(|) 它们来指定。如 re.I | re.M 被设置成 I 和 M 标志：</p>
        <table class="reference">
            <tbody><tr><th style="width:25%">修饰符</th><th>描述</th></tr>
            <tr><td>re.I</td><td>使匹配对大小写不敏感</td></tr>
            <tr><td>re.L</td><td>做本地化识别（locale-aware）匹配</td></tr>
            <tr><td>re.M</td><td>多行匹配，影响 ^ 和 $</td></tr>
            <tr><td>re.S</td><td>使 . 匹配包括换行在内的所有字符</td></tr>
            <tr><td>re.U</td><td>根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.</td></tr>
            <tr><td>re.X</td><td>该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。</td></tr>
            </tbody></table>

        <h2>正则表达式模式</h2>
        <p>模式字符串使用特殊的语法来表示一个正则表达式：</p>

        <p>字母和数字表示他们自身。一个正则表达式模式中的字母和数字匹配同样的字符串。</p>

        <p>多数字母和数字前加一个反斜杠时会拥有不同的含义。</p>

        <p>标点符号只有被转义时才匹配自身，否则它们表示特殊的含义。</p>

        <p>反斜杠本身需要使用反斜杠转义。</p>

        <p>由于正则表达式通常都包含反斜杠，所以你最好使用原始字符串来表示它们。模式元素(如 <span class="marked">r'\t'</span>，等价于 <span class="marked">\\t</span> )匹配相应的特殊字符。</p>
        <p>下表列出了正则表达式模式语法中的特殊元素。如果你使用模式的同时提供了可选的标志参数，某些模式元素的含义会改变。</p>
        <table class="reference">
            <tbody><tr><th style="width:25%">模式</th><th>描述</th></tr>
            <tr><td>^</td><td>匹配字符串的开头</td></tr>
            <tr><td>$</td><td>匹配字符串的末尾。</td></tr>
            <tr><td>.</td><td>匹配任意字符，除了换行符，当re.DOTALL标记被指定时，则可以匹配包括换行符的任意字符。</td></tr>
            <tr><td>[...]</td><td>用来表示一组字符,单独列出：[amk] 匹配 'a'，'m'或'k'</td></tr>
            <tr><td>[^...]</td><td>不在[]中的字符：[^abc] 匹配除了a,b,c之外的字符。</td></tr>
            <tr><td>re*</td><td>匹配0个或多个的表达式。</td></tr>
            <tr><td>re+</td><td>匹配1个或多个的表达式。</td></tr>
            <tr><td>re?</td><td>   匹配0个或1个由前面的正则表达式定义的片段，非贪婪方式</td></tr>
            <tr><td>re{ n}</td><td></td></tr>
            <tr><td>re{ n,}</td><td>精确匹配n个前面表达式。</td></tr>
            <tr><td>re{ n, m}</td><td>匹配 n 到 m 次由前面的正则表达式定义的片段，贪婪方式</td></tr>
            <tr><td>a| b</td><td>匹配a或b</td></tr>
            <tr><td>(re)</td><td>G匹配括号内的表达式，也表示一个组</td></tr>
            <tr><td>(?imx)</td><td>正则表达式包含三种可选标志：i, m, 或 x 。只影响括号中的区域。</td></tr>
            <tr><td>(?-imx)</td><td>正则表达式关闭 i, m, 或 x 可选标志。只影响括号中的区域。</td></tr>
            <tr><td>(?: re)</td><td> 类似 (...), 但是不表示一个组</td></tr>
            <tr><td>(?imx: re)</td><td>在括号中使用i, m, 或 x 可选标志</td></tr>
            <tr><td>(?-imx: re)</td><td>在括号中不使用i, m, 或 x 可选标志</td></tr>
            <tr><td>(?#...)</td><td>注释.</td></tr>
            <tr><td>(?= re)</td><td>前向肯定界定符。如果所含正则表达式，以 ... 表示，在当前位置成功匹配时成功，否则失败。但一旦所含表达式已经尝试，匹配引擎根本没有提高；模式的剩余部分还要尝试界定符的右边。</td></tr>
            <tr><td>(?! re)</td><td>前向否定界定符。与肯定界定符相反；当所含表达式不能在字符串当前位置匹配时成功</td></tr>
            <tr><td>(?&gt; re)</td><td>匹配的独立模式，省去回溯。</td></tr>
            <tr><td>\w</td><td> 匹配字母数字</td></tr>
            <tr><td>\W</td><td>匹配非字母数字</td></tr>
            <tr><td>\s</td><td> 匹配任意空白字符，等价于 [\t\n\r\f].</td></tr>
            <tr><td>\S</td><td>匹配任意非空字符</td></tr>
            <tr><td>\d</td><td> 匹配任意数字，等价于 [0-9].</td></tr>
            <tr><td>\D</td><td>匹配任意非数字</td></tr>
            <tr><td>\A</td><td>匹配字符串开始</td></tr>
            <tr><td>\Z</td><td>匹配字符串结束，如果是存在换行，只匹配到换行前的结束字符串。c</td></tr>
            <tr><td>\z</td><td>匹配字符串结束</td></tr>
            <tr><td>\G</td><td>匹配最后匹配完成的位置。</td></tr>
            <tr><td>\b</td><td>匹配一个单词边界，也就是指单词和空格间的位置。例如， 'er\b' 可以匹配"never" 中的 'er'，但不能匹配 "verb" 中的 'er'。</td></tr>
            <tr><td>\B</td><td>匹配非单词边界。'er\B' 能匹配 "verb" 中的 'er'，但不能匹配 "never" 中的 'er'。</td></tr>
            <tr><td>\n, \t, 等.</td><td>匹配一个换行符。匹配一个制表符。等</td></tr>
            <tr><td>\1...\9</td><td>匹配第n个分组的内容。</td></tr>
            <tr><td>\10</td><td>匹配第n个分组的内容，如果它经匹配。否则指的是八进制字符码的表达式。</td></tr>
            </tbody></table>
        <h2>
        正则表达式实例
    </h2>
        <h4>字符匹配</h4>
        <table class="reference">
            <tbody><tr><th style="width:25%">实例</th><th>描述</th></tr>
            <tr><td>python</td><td>匹配 "python". </td></tr>
            </tbody></table>
        <h4>字符类</h4>
        <table class="reference">
            <tbody><tr><th style="width:25%">实例</th><th>描述</th></tr>
            <tr><td>[Pp]ython </td><td>匹配 "Python" 或 "python"</td></tr>
            <tr><td>rub[ye]</td><td>匹配 "ruby" 或 "rube"</td></tr>
            <tr><td>[aeiou]</td><td>匹配中括号内的任意一个字母</td></tr>
            <tr><td>[0-9]</td><td>匹配任何数字。类似于 [0123456789]</td></tr>
            <tr><td>[a-z]</td><td>匹配任何小写字母</td></tr>
            <tr><td>[A-Z]</td><td>匹配任何大写字母</td></tr>
            <tr><td>[a-zA-Z0-9]</td><td>匹配任何字母及数字</td></tr>
            <tr><td>[^aeiou]</td><td>除了aeiou字母以外的所有字符
            </td></tr>
            <tr><td>[^0-9]</td><td>匹配除了数字外的字符
            </td></tr>
            </tbody></table>

        <h4>特殊字符类</h4>
        <table class="reference">
            <tbody><tr><th style="width:25%">实例</th><th>描述</th></tr>
            <tr><td>.</td><td>匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符，请使用象 '[.\n]' 的模式。</td></tr>
            <tr><td>\d</td><td>匹配一个数字字符。等价于 [0-9]。</td></tr>
            <tr><td>\D </td><td>匹配一个非数字字符。等价于 [^0-9]。</td></tr>
            <tr><td>\s</td><td>匹配任何空白字符，包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。</td></tr>
            <tr><td>\S </td><td>匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。</td></tr>
            <tr><td>\w</td><td>匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。</td></tr>
            <tr><td>\W</td><td>匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。</td></tr>
            </tbody></table>

    </div>
</div>
</body>
</html>
